package java_option.player.javafx.controller;

import java.io.File;

import org.bytedeco.javacv.FFmpegFrameGrabber;

import java_option.log.Logger;
import java_option.player.javacv.core.AudioPlayThread;
import java_option.player.javacv.core.FrameAudioGrabberThread;
import java_option.player.javacv.core.FrameFIFO;
import java_option.player.javacv.core.FrameVideoGrabberThread;
import java_option.player.javacv.core.VideoPlayThread;
import java_option.player.javacv.ext.AudioSoundUpdate;
import java_option.player.javacv.ext.VideoDurationUpdate;
import java_option.player.javacv.ext.VideoSliderUpdate;
import java_option.player.javacv.util.JavacvUtil;
import java_option.player.javafx.MainApplication;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.WindowEvent;

public class MainController {

	final String tag = "MainController";

	@FXML
	private Canvas mCanvas;
	@FXML
	private Button mOpenFileButton;

	// 以下是底部按钮
	@FXML
	private Slider mProgressSlider;
	@FXML
	private HBox mBottonHbox2;

	@FXML // 播放
	private Button mPlayButton;
	@FXML // 暂停
	private Button mStopButton;
	@FXML // 声音开关
	private Button mSoundSwitch;
	@FXML // 声音slider
	private Slider mSoundSlider;
	@FXML
	private Label mPlayedDurationLabel;
	@FXML // 已播放时间、和视频总时长之间的 分隔符
	private Label mDurationTag;
	@FXML
	private Label mTotalDurationLabel;

	// 当前正在播放的音频文件(磁盘路径 )
	private File currentVideoFile = null;

	public MainController() {

	}

	public void initialize() {
		// 1、初始化时长显示控件
		VideoDurationUpdate.getInstance().init(mPlayedDurationLabel, mDurationTag, mTotalDurationLabel);
		// 2、 初始化 声音大小显示控件
		AudioSoundUpdate.getInstance().init(mSoundSwitch, mSoundSlider);
		// 3、初始化音频进度条
		VideoSliderUpdate.getInstance().init(mProgressSlider);
		// 4、 设置视频进度条的宽度
		MainApplication.primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent window) {
				double width = MainApplication.primaryStage.getWidth() / 3;
				System.out.println(width);
				mProgressSlider.setMinWidth(width + (width - mBottonHbox2.getWidth()) / 2);
				mProgressSlider.setMaxWidth(width + (width - mBottonHbox2.getWidth()) / 2);
			}
		});
	}

	@FXML
	public void openFileAction(ActionEvent event) {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("选择文件");
		String file = "D:\\BaiduNetdiskDownload\\神探狄仁杰3.Amazing.Detective.Di.Renjie.III.S03.1080p.AMZN.WEB-DL.DDP2.0.H264-Ao";
//		file = System.getProperty("user.home");
		File file1 = new File(file);
		fileChooser.setInitialDirectory(file1);
		fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("mp4,avi", "*.mp4", "*.avi", "*.mkv"), //
				new FileChooser.ExtensionFilter("All FILE", "*.*"));
		MainApplication.primaryStage.setAlwaysOnTop(false);
		this.currentVideoFile = fileChooser.showOpenDialog(null);
		MainApplication.primaryStage.setAlwaysOnTop(true);
		if (this.currentVideoFile != null) {
			Logger.info("加载截图", "开始");
			this.mOpenFileButton.setVisible(false);
			WritableImage image = JavacvUtil.getImage(currentVideoFile.getPath(), 1);
			GraphicsContext gc = this.mCanvas.getGraphicsContext2D();
			gc.drawImage(image, 0, 0);
			Logger.info("加载截图", "结束");
		}
	}

	class PlayTask extends Task<Void> {

		@Override
		protected Void call() throws Exception {
			mOpenFileButton.setVisible(false);

			String file = currentVideoFile.getPath();
			// 视频 读取、播放
			FFmpegFrameGrabber videoGrabber = new FFmpegFrameGrabber(file);
			FrameVideoGrabberThread.getInstance().startGrab(videoGrabber);
			VideoPlayThread.getInstance().startPlay(mCanvas);

			// 音频 读取、播放
			FFmpegFrameGrabber audioGrabber = new FFmpegFrameGrabber(file);
			FrameAudioGrabberThread.getInstance().startGrab(audioGrabber);
			AudioPlayThread.getInstance().startPlay(audioGrabber);

			return null;
		}
	};

	@FXML
	public void onPlayAction(ActionEvent event) {
		if (currentVideoFile == null) {
			return;
		}
		// 播放--> 单击变暂停
		if (mPlayButton.getText().equals("播放")) {
			mPlayButton.setText("暂停");
			new Thread(new PlayTask()).start();
		}
		// 暂停 --> 单击变继续
		else if (mPlayButton.getText().equals("暂停")) {
			mPlayButton.setText("继续");
			VideoPlayThread.getInstance().setRunning(false);
			AudioPlayThread.getInstance().setRunning(false);
		}
		// 继续 --> 单击变暂停
		else {
			mPlayButton.setText("暂停");
			VideoPlayThread.getInstance().setRunning(true);
			AudioPlayThread.getInstance().setRunning(true);
		}
	}

	@FXML
	public void onStopAction(ActionEvent event) {
		VideoPlayThread.getInstance().setRunning(null);
		AudioPlayThread.getInstance().setRunning(null);
		// 清空列表
		FrameFIFO.getInstance().clearAll();
		FrameVideoGrabberThread.getInstance().setRunning(null);
		FrameAudioGrabberThread.getInstance().setRunning(null);

		this.currentVideoFile = null;
		// 隐藏播放时长Label
		this.mPlayedDurationLabel.setVisible(false);
		this.mTotalDurationLabel.setVisible(false);
		// 初始化 播放进度slider
		this.mProgressSlider.setValue(0);
		VideoSliderUpdate.getInstance().init(mProgressSlider);
		// 初始化播放按钮
		this.mPlayButton.setText("播放");
		// 显示打开文件按钮
		this.mOpenFileButton.setVisible(true);
	}

}
